﻿using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;

namespace BlazorAppDemo.Common
{
    public class ClientSettings
    {
        static string key = "myKey";

        public bool IsDarkModel { get; set; } = false;
        public string LastAccount { get; set; }

        ProtectedLocalStorage localStorage;

        static ClientSettings instance;

        public static ClientSettings Instance => instance;

        static ClientSettings()
        {
            instance = new ClientSettings();
        }

        public static async Task SetStorage(ProtectedLocalStorage localStorage)
        {
            if (instance.localStorage == null)
            {
                instance.localStorage = localStorage;
                try
                {
                    var val = await localStorage.GetAsync<ClientSettings>(key);
                }
                catch
                {
                    instance = new ClientSettings();
                }
            }
        }
        public async void Save()
        {
            try
            {
                if (localStorage == null) return;
                var json = this.ToJson();
                await localStorage.SetAsync(key, json);
            }
            catch
            {
            }
        }
        /// <summary>
        /// 浏览器向后一页
        /// </summary>
        public Action ToBack { get; set; }
    }
}
